package com.misty.admin.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.misty.admin.service.MessageService;
import com.misty.common.bean.Result;
import com.misty.common.constants.ResultEnum;
import com.misty.dao.domain.Message;
import com.misty.dao.dto.MessageDto;
import com.misty.dao.mapper.MessageMapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author cthulhu
 * @since 2022-11-21
 */
@Service
public class MessageServiceImpl extends ServiceImpl<MessageMapper, Message> implements MessageService {

    @Resource
    private MessageMapper messageMapper;

    /**
     *
     * @Param
     * @return
     * @Description 新增回复
     * @Date
     **/
    @Override
    public Result<?> saveMessage(Message message) {
        if(message.getUserId() == null){
            message.setUserId(3); //admin
        }
        message.setCreateTime(LocalDateTime.now());
        message.setUpdateTime(LocalDateTime.now());
        int insert = messageMapper.insert(message);
        if(insert <= 0){
            Result.error(ResultEnum.FAILURE.getValue(),"新增回复失败！");
        }
        return Result.success(ResultEnum.SUCCESS.getValue(),"新增回复成功！");
    }

    /**
     *
     * @Param
     * @return
     * @Description 更新回复
     * @Date
     **/
    @Override
    public Result<?> updateMessage(Message message) {
        message.setUpdateTime(LocalDateTime.now());
        int update = messageMapper.updateById(message);
        if(update <= 0){
            Result.error(ResultEnum.FAILURE.getValue(),"更新回复失败！");
        }
        return Result.success(ResultEnum.SUCCESS.getValue(),"更新回复成功！");
    }


    /**
     *
     * @Param
     * @return
     * @Description 删除回复
     * @Date
     **/
    @Override
    public Result<?> deleteMessage(Integer messageId) {
        int delete = messageMapper.deleteById(messageId);
        if(delete <= 0){
            Result.error(ResultEnum.FAILURE.getValue(),"删除回复失败！");
        }
        return Result.success(ResultEnum.SUCCESS.getValue(),"删除回复成功！");
    }

    /**
     *
     * @Param
     * @return
     * @Description 分页查询回复
     * @Date
     **/
    @Override
    public Result<?> Page(Integer pageNum, Integer pageSize, String search
    ) {
        LambdaQueryWrapper<Message> messageLambdaQueryWrapper = Wrappers.<Message>lambdaQuery();
        if(StrUtil.isNotBlank(search)){
            messageLambdaQueryWrapper.like(Message::getContent, search);
        }
        Page<MessageDto> messagePage = messageMapper.selectPageWithUser(new Page<>(pageNum, pageSize),messageLambdaQueryWrapper);
        if(messagePage.getTotal() <= 0){
            Result.error(ResultEnum.FAILURE.getValue(),"分页查询回复失败！");
        }
        return Result.success(ResultEnum.SUCCESS.getValue(),"分页查询回复成功！",messagePage);
    }

}
